/**
 * Provides commonly used utility functions defined in a static context.
 */
package Util;

public final class Util {

	public static void printStatusString(String s) {
		System.out.println(s);
	}
	
	public static void printWarningString(String w) {
		System.out.println(w);
	}
	
	public static void printWarningString(String w, Exception ex) {
		System.out.println(w);
		if (ex != null)
			ex.printStackTrace();
	}
	
	public static void printErrorString(String e) {
		System.out.println(e);
	}
	
	public static void printErrorString(String e, Exception ex) {
		System.out.println(e);
		ex.printStackTrace();
	}
	
	
	public static String isbn13(String isbn10) {
		
		int length = isbn10.length();
		System.out.println(isbn10+" is "+length+" chars long.");
		if (length == 13)
			return isbn10;
		else if (length == 2)
			return isbn10;
		else if (length == 0)
			return "";
		
		System.out.println("conversion needed.");
		String out = "";
		
		int[] digits = new int[9];
		
		// split isbn into digits
		for (int i = 0; i < 9; ++i) {
			digits[i] = Integer.parseInt(isbn10.substring(i, i+1));
			System.out.println(digits[i]);
		}
		
		// compute check digit
		int check = 38; // inital value = 9 + (7*3) + 8
		
		// add digits with appropriate multipliers:
		// odd indexed digits are multiplied by 1
		// even indexed digits are multiplied by 3
		for (int i = 0; i < 9; ++i)
			if (i % 2 == 0)
				check = check + 3*digits[i];
			else
				check = check + digits[i];
		
		check = check % 10; // divide by 10 and return the remainder 38 % 10 = 8
		check = (10 - check) % 10; // 10 - 8 = 2; 2 % 10 = 2
		
		// collect digits into isbn13
		
		out += "978";	// prefix
		
		for (int i = 0; i < 9; ++i)
			out += String.valueOf(digits[i]);
		
		out += String.valueOf(check);
		
		System.out.println("conversion result: "+out);
		return out;
	}
}
